<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">设置</a>
        <a>
          <cite>课程包管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-small" style="float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="wodekehu" class="x-body" class="clear:both;">
    <div class="site-demo-button" style="margin-bottom: 0;" lay-filter="selBtn">
        <button class="layui-btn site-demo-active" id="course">课程管理</button>
        <button class="layui-btn site-demo-active layui-btn-warm" id="course_package">课程包管理</button>
    </div>
    <div class="layui-row"
         style="padding-left:20px;border-left:1px solid #e5e5e5;padding-top:20px;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">
            <div class="layui-input-inline">
                <select name="course_ascription" lay-verify="required" lay-search="">
                    <option value="">课程归属</option>
                    {volist name="data[11]" id="v"}
                    <option value="{$v.id}">{$v['name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="year" lay-verify="required" lay-search="">
                    <option value="">所属年份</option>
                    {volist name="data[12]" id="v"}
                    <option value="{$v.id}">{$v['name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="grade" lay-verify="required" lay-search="">
                    <option value="">适用年级</option>
                    {volist name="data[1]" id="v"}
                    <option value="{$v.id}">{$v['name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <input type="text" name="course_name" placeholder="课程名称" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" data-type="reload" id="search">筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">重置</button>
                <button class="layui-btn" id="add"><i class="layui-icon">&#xe608;</i>新增课程包</button>
            </div>
        </div>
    </div>

    <table class="layui-table" id="datalist" lay-filter="Event"></table>

    <script type="text/html" id="barTable">
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">修改</a>{{# if(d.status == 0){ }}
        <a class="layui-btn layui-btn-xs" style="background-color: #009688" lay-event="check">启用</a>{{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="check">禁用</a>{{# } }}
    </script>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table', 'element', 'laydate', 'layer'], function () {
        var table = layui.table,
            laydate = layui.laydate,
            element = layui.element,
            layer = layui.layer,
            $ = layui.$;

        $('#add').click(function () {
            var url = "{:url('package/course')}";
            layer.open({
                type: 2,
                title: '选择课程',
                shadeClose: true,
                shade: 0.5,
                area: ['90%', '50%'],
                content: url
            });
        });

        $('#course').click(function () {
            var url = "{:url('course/course_manage')}";
            location.href = url;
        });

        $('#course_package').click(function () {
            var url = "{:url('package/package_manage')}";
            location.href = url;
        });

        //方法级渲染
        var tableIns = table.render({
            elem: '#datalist',
            url: '{:url("package/package_manage")}',
            method: 'POST',
            cols: [[
                {field: 'id', title: '编号', width: 100, sort: true, fixed: false, align: 'center'},
                {field: 'name', title: '课程包名称', width: 130, align: 'center'},
                {field: 'course_ascription', title: '课程归属', width: 130, align: 'center'},
                {field: 'year', title: '年份', width: 70, align: 'center'},
                {field: 'grade', title: '年级', width: 80, align: 'center'},
                {field: 'course', title: '包含课程', width: 130, align: 'center'},
                {field: 'totalprice', title: '总金额(元)', width: 100, align: 'center'},
                {field: 'discount', title: '优惠(元)', width: 100, align: 'center'},
                {field: 'receivable', title: '应收(元)', width: 100, align: 'center'},
                {field: 'founder', title: '创建人', width: 80, align: 'center'},
                {field: 'create_date', title: '创建时间', width: 110, align: 'center'},
                {field: 'status_text', title: '状态', width: 80, align: 'center'},
                {title: '操作', fixed: 'right', width: 120, align: 'center', toolbar: '#barTable'},
            ]],
            page: true,
            height: 'full-240',
            done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            }
        });


        //监听工具条
        table.on('tool(Event)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'edit') {
                //跟进
                var url = '{:url("package/edit")}?id=' + data.id;
                location.href = url;
            } else if (layEvent === 'check') {
                var status;
                if (data.status == 0) {
                    status = 1;
                } else {
                    status = 0;
                }
                //激活与禁用状态改变
                $.post("{:url('package/package_check_status')}", {
                    id: data.id,
                    status: status
                }, function (rJson) {
                    if (rJson.code === 0) {
                        layer.msg(rJson.msg, {
                            time: 2000
                        });
                        setTimeout(function () {
                            location.reload();
                        }, 2000)
                    } else {
                        layer.msg(rJson.msg, {
                            icon: 2,
                            time: 2000
                        });
                    }
                }, "json")
            }
        });
        //搜索项
        $('#search').click(function () {
            var field = {};
            field.grade = $('select[name=grade] option:selected').val();
            field.course_ascription = $('select[name=course_ascription] option:selected').val();
            field.year = $('select[name=year] option:selected').val();
            field.course_name = $('input[name=course_name]').val();
            console.log(field);
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //清除
        $('#clear').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns.reload({
                where: {
                    'grade': '',
                    'course_ascription': '',
                    'year': '',
                    'course_name': '',
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>
<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
